<template>
  <a-modal
    :title="title"
    :width="800"
    :ok="false"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :okButtonProps="{ props: { disabled: disableSubmit } }"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="文件名称"
          prop="fileList"
          :hidden="false"
          hasFeedback
        >
          <!-- <j-upload v-model="fileList"  v-if="fileList.length===0"></j-upload>
          {{ fileList.length>0 ?fileList:""}} -->
          <j-upload v-model="fileList"  :disabled="fileList.length>0?true:false" :buttonVisible="!(fileList.length>0?true:false)"></j-upload>
          
        </a-form-model-item>
       
      </a-form-model>
    </a-spin>
  </a-modal>
</template>
  
  <script>
import { httpAction2,httpAction } from '@/api/manage'
import { queryFileTreeList } from '@/api/api'
import { getFileAccessHttpUrl } from '@/api/manage'
import pick from 'lodash.pick'
import signMd5Utils from '@/utils/encryption/signMd5Utils'
import ATextarea from 'ant-design-vue/es/input/TextArea'
import JUpload from  '@/components/jeecg/JUpload'
export default {
  name: 'SysDepartModal',
  components: { ATextarea, JUpload},
  data() {
    return {
      departTree: [],
      orgTypeData: [],
      fileList: [],
      directoryIds:'',
      phoneWarning: '',
      name: '',
      title: '操作',
      seen: false,
      visible: false,
      condition: true,
      disableSubmit: false,
      model: {
        name: '',
      },
      defaultModel: {
        type: '1',
      },
      menuhidden: false,
      menuusing: true,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },

      confirmLoading: false,
      validatorRules: {
        directoryName: [{ required: true, message: '请输入目录名称!' }],
        orgCode: [{ required: true, message: '请输入机构编码!' }],
        mobile: [{ validator: this.validateMobile }],
        orgCategory: [{ required: true, message: '请输入机构类型!' }],
      },
      url: {
        add: '/system/sysOrgFileDetail/add',
      },
      dictDisabled: true,
    }
  },
  created() {},
  methods: {
    loadTreeData() {
      var that = this
      queryFileTreeList().then((res) => {
        if (res.success) {
          that.departTree = []
          for (let i = 0; i < res.result.length; i++) {
            let temp = res.result[i]
            that.departTree.push(temp)
          }
        }
      })
    },
    add(depart) {
     this.loadTreeData()
      this.visible = true
     this.directoryIds=depart
      // if(depart){
      //   this.seen = false;
      //   this.dictDisabled = false;
      // }else{
      //   this.seen = true;
      //   this.dictDisabled = true;
      // }
      // this.edit(depart);
    },
    edit(record) {
      this.visible = true
      this.model = Object.assign({}, this.defaultModel, record)
      this.loadTreeData()
      // console.log('fsg',record);
      this.model.parentId = record != null ? record.toString() : null
      if (this.seen) {
        this.model.type = '1'
      } else {
        this.model.type = '2'
      }
    },
    close() {
      this.$emit('close')
      this.disableSubmit = false
      this.visible = false
      this.$refs.form.resetFields()
    },
    handleOk() {
      //let url  = "http://124.70.204.126:8080/jeecg-boot/system/sysOrgFileDetail/add"
      let url  = "/system/sysOrgFileDetail/add"
      let sign = signMd5Utils.getSign(url);
      let signHeader = {"X-Sign": sign,"X-TIMESTAMP": signMd5Utils.getTimestamp(), 'Content-Type': 'multipart/form-data'};
     // let signHeader = {'Content-Type': 'multipart/form-data'};
      const that = this
      that.fileLists = getFileAccessHttpUrl(that.fileList)
    //  console.log('few',that.fileList.length);
      var fileName=that.fileLists
      var  strFileName=fileName.replace(/(.*\/)*([^.]+).*/ig,"$2");
      var strFileName1=strFileName.replace(/_(\S*)/, "")
      var FileExt=fileName.replace(/.+\./,"");
     var filesName=strFileName1+'.'+FileExt
      // console.log('bfd',strFileName);
      // console.log('bfd1',strFileName1);

      
      // console.log('bhd',filesName);
      // console.log('sfa',that.fileLists);
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
      
          httpAction(
            url,
            {
              name:filesName,
              orgFileId: that.directoryIds,
              fileUrl: that.fileLists,
            },
            'post',
            
          )
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.loadTreeData()
                that.$emit("ok")
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
              that.fileList=[]
            })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
      this.fileList=[]
    },
  },
}
</script>
  
  <style scoped>
</style>